//////////////////////////////////////////////////////////////////////////

#ifndef _SPLASHSCREEN_H_
#include "splashscreen.h"
#endif

//////////////////////////////////////////////////////////////////////////

#ifndef _INPUT_H_
#include "input.h"
#endif

#ifndef _TEXTSURFACE_H_
#include "textsurface.h"
#endif

#ifndef _COLORS_H_
#include "colors.h"
#endif

#ifndef _FONTS_H_
#include "fonts.h"
#endif

#ifndef _SOUNDS_H_
#include "sounds.h"
#endif

#ifndef _THEME_H_
#include "theme.h"
#endif

#ifndef _GUI_H_
#include "gui.h"
#endif

#ifndef _TIMER_H_
#include "timer.h"
#endif

#include <SDL\SDL_ttf.h>
#include <SDL\SDL.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <SDL_mixer.h>
//////////////////////////////////////////////////////////////////////////


char *SplashTitles[5] = {"Anastasia Karpinska", "Paul Parchenko", "Ruslan Kovalchuk", "present", "Shoot the Music"};


//////////////////////////////////////////////////////////////////////////


GUI_SplashScreen::GUI_SplashScreen(GUI *_gui) : 
    m_GUI(_gui),
    m_CurrentTitle(-1),
    m_TitleSurface(0),
    m_Waiting(false)
{
    Mix_PlayMusic(GUI_Theme::GetMusic(GUI_Sounds::Intro), -1);

    AddEventToObserve(Observer::KEY_INPUT_CHANGE);
    AddEventToObserve(Observer::MOUSE_POSITION_CHANGE);
}


//////////////////////////////////////////////////////////////////////////


GUI_SplashScreen::~GUI_SplashScreen()
{
    Mix_HaltMusic();
}


//////////////////////////////////////////////////////////////////////////


void GUI_SplashScreen::Render(SDL_Surface *_mainSurface)
{
    if (!m_Waiting)
    {
        if (m_CurrentTitle < 4)
        {
            m_CurrentTitle++;

            if(SDL_MUSTLOCK(_mainSurface)) 
            {
                if(SDL_LockSurface(_mainSurface) < 0) return;
            }

            if (m_TitleSurface)
                delete m_TitleSurface;
            SDL_Color back = GUI_Theme::Color(GUI_Colors::SplashBackground);
            
            SDL_FillRect(_mainSurface, &_mainSurface->clip_rect, SDL_MapRGB(_mainSurface->format, back.r, back.g, back.b));
            m_TitleSurface = new GUI_TextSurface(GUI_Fonts::SplashName, GUI_Colors::SplashText, SplashTitles[m_CurrentTitle]);
            m_TitleSurface->Blt(_mainSurface, 0.5, 0.5);
            if(SDL_MUSTLOCK(_mainSurface)) SDL_UnlockSurface(_mainSurface);
            SDL_Flip(_mainSurface); 

            m_Waiting = true;
            m_Delay = Timer::Reprieve(2000);

        } else
            m_GUI->SwitchToScreen(GUI::Menu);    
    } else
    {
        m_Waiting = Timer::ItsTime(m_Delay);
    }
}


//////////////////////////////////////////////////////////////////////////


void GUI_SplashScreen::Resize()
{
    if (m_TitleSurface)
        m_TitleSurface->Resize();
}


//////////////////////////////////////////////////////////////////////////


void GUI_SplashScreen::OnKeyChanged(Input* _input)
{
//    static int asd = 0;
    Input::Key key = _input->GetKey(); 
    if ((key.m_State == Input::RELEASED) || (key.m_State == Input::MOUSE_RELEASED))
    {
        switch (key.m_Code)
        {
        case Input::RETURN:
        case Input::SPACE:
        case Input::ESCAPE:
            m_GUI->SwitchToScreen(GUI::Menu);
            break;
        case Input::MOUSE1:
            m_Waiting = false;
            break;
        case Input::EXIT:
            m_GUI->SwitchToScreen(GUI::Exit);
            break;

        default:
            //assert(0);
            break;
        }
    }
}


//////////////////////////////////////////////////////////////////////////


void GUI_SplashScreen::OnMousePositionChanged(Input* _input)
{
    Input::MousePosition pos = _input->GetMousePosition();
    // do something
}


//////////////////////////////////////////////////////////////////////////
